package fr.uparis10.miage.m1p1.employes;

import java.util.HashMap;

import fr.uparis10.miage.m1p1.core.Departement;

public class DirecteurDepartement extends Manager {

	/* The next attribute will just hold the maximum allowed budget for a specific application */
	private static final HashMap<String, Integer> MAX_BUDGET;
	static {
		MAX_BUDGET = new HashMap<String, Integer>();
		MAX_BUDGET.put("PROJECT", 100000);
		MAX_BUDGET.put("TEAM", 10000);
		MAX_BUDGET.put("RELOCATION", 20000);
		MAX_BUDGET.put("TRAVEL", 30000);
		MAX_BUDGET.put("OTHER", 1000);
	}
	
	/* This variable will hold the history of budget allocation */
	private HashMap <String, Integer> m_usedBudget = new HashMap<String, Integer>();

	/**
	 * This static method is used to exchange the department for 2 Directeurs 
	 * @param dir1
	 * @param dir2
	 * @return
	 */
	private static boolean exchangeDepartments(DirecteurDepartement dir1, DirecteurDepartement dir2) {
		if (dir1 == null || dir2 == null) {
			return false;
		}
		
		Departement auxDept = dir1.m_dept;
		dir1.m_dept = dir2.m_dept;
		dir2.m_dept = auxDept;

		return true;
	}

	/**
	 * Used to request some budget.
	 * @param requestedBudget
	 * @param reason
	 * @return the requested budget, or less
	 */
	@Override
	public int requestBudget(int requestedBudget, String reason) {
		Integer usedBudget = m_usedBudget.get(reason);
		if (usedBudget == null) {
			usedBudget = 0;
		}
		Integer maxBudget = MAX_BUDGET.get(reason);
		if (maxBudget == null) {
			maxBudget = 0;
		}
		int availableBudget = maxBudget - usedBudget; 
		
		int allowedBudget = availableBudget < requestedBudget ? availableBudget : requestedBudget;
		usedBudget += allowedBudget;
		m_usedBudget.put(reason, usedBudget);
		
		return allowedBudget;
	}

	
	/**
	 * This method calculates the bonus for which the Director is eligible
	 * @return the bonus value 
	 */
	@Override
	public int calcBonus() {
		/* As of now, this is always "0" */
		return 0;
	}
	
	/**
	 * Used to change the department with another Director
	 * @param newDept - the Departement that this Director will take over
	 * @return
	 */
	public boolean changeDepartement(Departement newDept) {
		DirecteurDepartement currentDirector;
		if (newDept == null || (currentDirector = newDept.getDirecteur()) == null) {
			return false;
		}
		
		return exchangeDepartments(this, currentDirector);
	}

	/**
	 * Overrides the setter of the father (Employe) method.
	 * Will exchange
	 */
	@Override
	public void setDept(Departement dept) {
		if (!changeDepartement(dept)) {
			super.setDept(dept);
		}
	}
}
